package com.office.shop.by.components;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Олег Семенов
 * Date: 05.07.11
 * Time: 10:28
 * To change this template use File | Settings | File Templates.
 */
public class CHttp {

       private static String buildUrl(String url)  {
            String specChar = "?";
            if(url.indexOf('?')>0)
                    specChar = "&";

            if(CConfig.phpDebug)
                return  url + specChar + "XDEBUG_SESSION_START=" + CConfig.phpDebugId  ;
            else
               return  url;
       }

       public static JSONObject getJSON(String url) {
                JSONObject json = null;
                String result = get(buildUrl(url));
                try
                {
                    json = new JSONObject(result);
                }
                catch (JSONException e){
                   return null;
                }
                return json;
        }

       public static String get(String url) {
                String result = "";
                HttpClient httpclient = new DefaultHttpClient();
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                HttpGet httpget = new HttpGet(url);
                HttpResponse response;
                try {
                        response = httpclient.execute(httpget);
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                                InputStream instream = entity.getContent();
                                result = convertStreamToString(instream);
                                instream.close();
                        }
                } catch (ClientProtocolException e) {
                } catch (IOException e) {
                }
                return result;
       }

       public static String post(String url,List<NameValuePair> paramList) {
                String result = "";
                String cacheId =  CCache.generateCacheId(url,paramList);
                result = CCache.getString(cacheId);
                if(result != null)
                {
                    return result;
                }
                else
                {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response;
                try {
                       if(paramList != null)
                            httppost.setEntity(new UrlEncodedFormEntity(paramList));

                        response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                                InputStream instream = entity.getContent();
                                result = convertStreamToString(instream);
                                instream.close();
                        }
                } catch (ClientProtocolException e) {
                    return null;
                } catch (IOException e) {
                    return null;
                }
                    CCache.putString(cacheId,result);
                    return result;
                }
        }

       public static JSONObject postJSON(String url, List<NameValuePair> paramList) {
                JSONObject json = null;
                String result = post(buildUrl(url), paramList);
                try {
                        json = new JSONObject(result);
                } catch (JSONException e) {
                }
                return json;
        }

       private static String convertStreamToString(InputStream is) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = null;
                try {
                        while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                is.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                return sb.toString();
        }
}
